<script setup lang="ts">
import { usePrintStore } from '@/stores'
import type { FormInstance, FormRules } from 'element-plus'
import { DY_MAP_VisibleScales } from '@/constants'

const printtingStore = usePrintStore()
const emit = defineEmits(['update:modelValue'])
const basicInfoForm = reactive<BasicInfo>({
  layoutTemplate: 'A4_landscape',
  title: '德阳市地图',
  subtitle: '',
  copyright: '',
  author: '北京超图软件股份有限公司'
})
const basicInfoFormRef = ref<FormInstance>()
const rules = reactive<FormRules<BasicInfo>>({
  layoutTemplate: [{ required: true, message: '请选择布局模板', trigger: 'blur' }],
  title: [{ required: true, message: '请填写标题信息', trigger: 'blur' }],
  author: [{ required: true, message: '请填写作者信息', trigger: 'blur' }]
})

async function submitBasicInfoForm(formEl: FormInstance | undefined, type: string) {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (!valid) return
    printtingStore.submitBasicInfoForm(basicInfoForm)
    emit('update:modelValue', type)
    const { lng, lat } = map.getCenter()
    const zoom = map.getZoom()
    printtingStore.additionMapScale = DY_MAP_VisibleScales[Math.round(zoom) - 2 < 0 ? 0 : Math.round(zoom) - 2].toString()
    printtingStore.centerPoint = `${lng},${lat}`
  })
}

watch(
  () => printtingStore.basicInfo,
  (newVal) => Object.assign(basicInfoForm, newVal)
)
</script>

<template>
  <div>
    <el-form ref="basicInfoFormRef" :rules="rules" :model="basicInfoForm" label-position="right">
      <el-form-item label="布局模板：" prop="layoutTemplate" label-width="100px">
        <el-select v-model="basicInfoForm.layoutTemplate" placeholder="布局模板选择" style="width: 300px">
          <el-option label="A2_landscape" value="A2_landscape" />
          <el-option label="A3_portrait" value="A3_portrait" />
          <el-option label="A4_landscape" value="A4_landscape" />
          <el-option label="A4_portrait" value="A4_portrait" />
          <el-option label="A2_portrait" value="A2_portrait" />
          <el-option label="A3_landscape" value="A3_landscape" />
          <el-option label="custom" value="custom" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题：" prop="title" label-width="100px">
        <el-input v-model="basicInfoForm.title" autocomplete="off" style="width: 300px" />
      </el-form-item>
      <el-form-item label="副标题：" prop="subtitle" label-width="100px">
        <el-input v-model="basicInfoForm.subtitle" autocomplete="off" style="width: 300px" />
      </el-form-item>
      <el-form-item label="版权信息：" prop="copyright" label-width="100px">
        <el-input v-model="basicInfoForm.copyright" autocomplete="off" style="width: 300px" />
      </el-form-item>
      <el-form-item label="作者：" prop="author" label-width="100px">
        <el-input v-model="basicInfoForm.author" autocomplete="off" style="width: 300px" />
      </el-form-item>
    </el-form>
    <div class="btn-group">
      <el-button type="warning" @click="printtingStore.setFalse()" style="margin-right: 50px">取消</el-button>
      <el-button type="primary" @click="submitBasicInfoForm(basicInfoFormRef, 'additionInfo')">下一步</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.btn-group {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
